﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using PcProject.Domain.IRepository;
using PcProject.Domain.Models.Entities;

namespace PcProject.Infrastructure.Repository
{
    public class DapperRepositoryBase<T> : IRepository<T> where T : class
    {
        //连接字符串
        protected string ConnStr = "";

        //配置类的接口
        protected IDbConnection Conn;


        /// <summary>
        /// 分页查询(返回集合数据和总条数)
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereCondition"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public async Task<AsyncProgram<T>> GetPageListAsync(int pageIndex, int pageSize, string whereCondition = "", object paras = null)
        {
            //返回总条数
            int totalCount = await Conn.RecordCountAsync<T>(whereCondition, paras);
            //分页结果集
            var data = await Conn.GetListPagedAsync<T>(pageIndex, pageSize, whereCondition, "", paras);
            //实例化
            AsyncProgram<T> program = new AsyncProgram<T>();
            program.Data = data;
            program.TotalCount = totalCount;
            return program;
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> ShowAsync()
        {
            return (List<T>)await Conn.GetListAsync<T>();
        }

        //查询
        /// <summary>
        /// 根据主键查询(返回单条数据)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetByIdAsync(int id)
        {
            return await Conn.GetAsync<T>(id);
        }

        /// <summary>
        /// 根据条件查(返回集合数据)
        /// </summary>
        /// <param name="whereCondition"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T>> GetListAsync(string whereCondition = "", object paras = null)
        {
            return await Conn.GetListAsync<T>(whereCondition, paras);
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(T obj)
        {
            return await Conn.InsertAsync<T>(obj) ?? -1; //??语法的意思是：当前面表示式为null的时候，返回？？后面的值;
        }

        /// <summary>
        /// 商品删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int id)
        {
            return await Conn.DeleteAsync<T>(id);
        }

        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T obj)
        {
            return await Conn.UpdateAsync<T>(obj);
        }

    }
}
